define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'resource/field/index' + location.search,
                    add_url: 'resource/field/add',
                    edit_url: 'resource/field/edit',
                    del_url: 'resource/field/del',
                    multi_url: 'resource/field/multi',
                    import_url: 'resource/field/import',
                    table: 'field',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                search:false,
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'city', title: __('City'), operate: 'LIKE'},
                        {field: 'supporting_services', title: __('Supporting_services'), operate: 'LIKE'},
                        {field: 'stay_number', title: __('Stay_number'),formatter:function(value,row,index){return value + "人" ; }},
                        {field: 'restaurant_number', title: __('Restaurant_number'),formatter:function(value,row,index){return value + "人" ; }},
                        {field: 'meeting_number', title: __('Meeting_number'),formatter:function(value,row,index){return value + "人" ; }},
                        {field: 'price', title: __('Price'), operate: '='},
                        {field: 'mobile', title: __('联系信息'), operate: false,align: 'left',formatter:function(value,row,index){return '<p>号码:'+value + "</p><p>地址:" + row.area+'</p><p>域名:'+row.url+'</p>'; }},
                        /*{field: 'admin.nickname', title: __('Principal'), operate: 'LIKE'},*/
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on('click','.types',function () {
                    var that = this
                    var type = $(that).data('type');
                    var search = $(that).data('search');
                    var val = $('#c-'+type+'_type').val();
                    var url = 'category/look?type=' +  type+'&val='+val+'&search='+search;
                    Fast.api.open(Fast.api.fixurl(url),search,{
                        callback:function(res){
                            console.log('res'+res)
                            $(that).val(res)
                            try {
                                if(typeof  callback === 'function'){
                                    callback.call(that,res)
                                }
                            }catch (e) {

                            }
                        }
                    })
                })
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
